import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:portal_website/tool/color_util.dart';

class ImageNetwork extends StatelessWidget {
  ImageNetwork(
    this.imageUrl, {
    Key? key,
    this.width,
    this.height,
    this.placeholderWidth,
    this.placeholderHeight,
    this.fit = BoxFit.fill,
    this.placeholderFit = BoxFit.contain,
    this.backColor = ColorsUtil.colorEEEEEE,
    this.useOldImageOnUrlChange = false,
    this.isDelete = 0,
    this.memCacheWidth,
    this.isOriginal = true,
    this.quality = 85,
    this.compressWidth,
    this.compressHeight,
    this.alignment = Alignment.center,
  }) : super(key: key);

  final String imageUrl;
  final double? width;
  final double? height;
  final double? placeholderWidth;
  final double? placeholderHeight;
  final BoxFit fit;
  final BoxFit placeholderFit;
  final bool? useOldImageOnUrlChange;
  final int? memCacheWidth;
  final Color? backColor;
  final int? isDelete;
  final bool isOriginal; //是否使用原图
  final int quality; //压缩图片质量
  int? compressWidth; //压缩图片宽度
  int? compressHeight; //压缩图片高度
  final Alignment alignment;

  @override
  Widget build(BuildContext context) {
    Widget emptyWidget = Container(
      width: placeholderWidth ?? (width ?? 0),
      height: placeholderHeight ?? (height ?? 0),
      alignment: Alignment.center,
      color: backColor,
    );
    if (isDelete == 1) {
      return emptyWidget;
    }
    if (imageUrl.isEmpty) {
      return emptyWidget;
    }
    if (!isOriginal) {
      if (compressWidth == null) {
        if (width != null) {
          compressWidth = width!.toInt();
        } else {
          compressWidth = 400;
        }
      }
      if (compressHeight == null) {
        if (height != null) {
          compressHeight = height!.toInt();
        } else {
          compressHeight = 300;
        }
      }
    }

    // return ExtendedImageNetwork(
    //   imageUrl,
    //   width: width,
    //   height: height,
    //   fit: fit,
    //   enableMemoryCache: true,
    //   loadStateChanged: (ExtendedImageState state) {
    //     switch (state.extendedImageLoadState) {
    //       case LoadState.failed:
    //         return emptyWidget;
    //       case LoadState.loading:
    //         return loadingType == ImageLoadingType.delete
    //             ? Container()
    //             : emptyWidget;
    //       case LoadState.completed:
    //         break;
    //     }
    //     return null;
    //   },
    // );
    return Image.network(imageUrl);

    // return CachedNetworkImage(
    //   alignment: alignment,
    //   // imageUrl: isOriginal
    //   //     ? imageUrl
    //   //     : "$imageUrl?imageView2/1/w/$compressWidth/h/$compressHeight/q/$quality",
    //   imageUrl: imageUrl,
    //   width: width,
    //   height: height,
    //   // memCacheWidth: isOriginal ? null : compressWidth,
    //   // memCacheHeight: isOriginal ? null : compressHeight,
    //   fit: fit,
    //   fadeInDuration: const Duration(seconds: 0),
    //   fadeOutDuration: const Duration(seconds: 0),
    //   placeholder: (context, url) {
    //     return emptyWidget;
    //   },
    //   errorWidget: (context, url, error) {
    //     return emptyWidget;
    //   },
    // );
  }
}
